package com.l024.android_ljl.view.login;

import android.Manifest;
import android.content.Intent;
import android.view.View;
import android.widget.RadioGroup;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivityRegisterBinding;
import com.l024.android_ljl.viewmodel.login.LoginViewModel;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.event.OnClickEvent;
import com.quantstack.llcommonlibrary.utils.GlideUtil;
import com.quantstack.llcommonlibrary.utils.LogUtils;
import com.quantstack.llcommonlibrary.utils.PermissionUtils;
import com.quantstack.llcommonlibrary.utils.PhotoUriUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

/**
 * 注册页面
 */
public class RegisterActivity extends BaseActivity<LoginViewModel, ActivityRegisterBinding> implements BaseViewModel.OnPageDataListener {

    //权限
    private final static String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA};

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void afterCreate() {
        mViewDataBind.setUserModel(mViewModel);
        mViewModel.setBaseView(this);
        mViewModel.setOnPageDataListener(this);
        //监听切换
        mViewDataBind.rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 你的代码
                if(R.id.rb_nan==checkedId){
                    mViewModel.setSex(1);
                }else{
                    mViewModel.setSex(2);
                }
            }
        });
        //上传头像
        mViewDataBind.ivAvator.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                checkPermission();
            }
        });
    }

    /**
     * 申请权限
     */
    private void checkPermission(){
        PermissionUtils.checkPermission(this, new PermissionUtils.PermissionListener() {
            @Override
            public void onPermissionGranted() {
                selectImg();
            }

            @Override
            public void onPermissionDenied(boolean disable) {
                //是否禁止询问
                if(disable){
                    //跳转到设置页面
                    PermissionUtils.gotoMiuiPermission(RegisterActivity.this);
                }else{
                    ToastUtils.show("请打开权限");
                    checkPermission();
                }
            }
        },permissions);
    }

    /**
     * 相册选择
     */
    private void selectImg(){
        PictureSelector
                .create(RegisterActivity.this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            if (data != null) {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                String path = "";
                if (pictureBean.isCut()) {
                    path = pictureBean.getPath();
                    GlideUtil.loadImage(this,path,mViewDataBind.ivAvator);

                    mViewModel.setAvator(path);
                } else {
                    path = PhotoUriUtils.getRealPathFromUri(this,pictureBean.getUri());
                    GlideUtil.loadImage(this,pictureBean.getUri(),mViewDataBind.ivAvator);

                    mViewModel.setAvator(path);
                }
                LogUtils.e("拍照的图片路径:"+mViewModel.getSysUser().getValue().toString());
                //上传图片
                mViewModel.uploadImage(path);
            }
        }
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.REGISTER:{
                ToastUtils.show("注册成功");
                go(this,LoginActivity.class);
                finish();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.REGISTER:{
                ToastUtils.show(msg);
                break;
            }
        }
    }
}